package com.haojiang.mh.widget.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.haojiang.mh.R;
import com.haojiang.mh.utils.SizeUtil;

/**
 * 雷达图 兼容五/六个指标
 */
public class RadarChartView extends View {
    private static final int mRadarLineCount = 5;
    private Paint mPaint = null;
    private TextPaint mTextPaint = null;
    private String[] mTexts = null;
    private int[] mPointColors = null;
    private float[] mValues = null;
    private int mHeight;
    private Point mCenterPoint;
    private int mTextOffSet;

    private float mAngle = 0;

    private int mLineColor;

    private int mValuePathColor;

    public RadarChartView(Context context) {
        this(context, null);
    }

    public RadarChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadarChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        mTexts = getResources().getStringArray(R.array.RadarArray_6);

        mPointColors = new int[]{
                R.color.purple_5,
                R.color.purple_2,
                R.color.yellow_start,
                R.color.purple_3,
                R.color.grey_3,
                R.color.green_light
        };

//        mLineColor = getResources().getColor(R.color.grey_3);
//        mValuePathColor = getResources().getColor(R.color.purple_4) & 0xb4ffffff;
        mLineColor = getResources().getColor(R.color.color_white);                                                      //文字颜色
        mValuePathColor = getResources().getColor(R.color.color_golden_dark_n) & 0xb4ffffff;        //雷达中间颜色

        mTextOffSet = SizeUtil.dp2px(getContext(), 5);

        mValues = new float[]{0, 0, 0, 0, 0, 0};

        mPaint = new Paint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);

        mTextPaint = new TextPaint();
        mTextPaint.setTextSize(SizeUtil.sp2px(getContext(), 12));
        mTextPaint.setColor(mLineColor);
    }

    public float getBaseLine(TextPaint textPaint, float baseLine) {
        Paint.FontMetrics fm = textPaint.getFontMetrics();
        float fontHeight = fm.bottom - fm.top;
        return baseLine - (textPaint.getTextSize() - fontHeight) / 2 - fm.bottom;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = Math.min(w, h);
        mCenterPoint = new Point(w / 2, h / 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //小圆圈单位半径
        float radius;
        radius = (mHeight - 2 * mTextPaint.getTextSize() - 6 * mTextOffSet) * 0.5f / 5.5f;
        //夹角
        mAngle = (float) (2 * Math.PI / mValues.length);

        mPaint.setColor(mLineColor);
        mPaint.setStyle(Paint.Style.STROKE);
        drawCircleRadar(canvas, radius);

        drawRadarLine(canvas, radius * 4.5f);

        drawText(canvas, radius * 5.5f);

        mPaint.setStyle(Paint.Style.FILL);
        drawPoints(canvas, radius * 5.5f - mTextOffSet);

        mPaint.setColor(mValuePathColor);
        drawValuePart(canvas, radius * 4);
    }


    private void drawCircleRadar(Canvas canvas, float unitRadius) {
        for (int i = 1; i < mRadarLineCount; i++) {
            canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, unitRadius * i, mPaint);
        }
    }

    /**
     * 直角三角形:
     * sin(x)是对边比斜边
     * cos(x)是底边比斜边
     * tan(x)是对边比底边
     * 推导出:底边(x坐标)=斜边(半径)*cos(夹角角度),对边(y坐标)=斜边(半径)*sin(夹角角度)
     */
    private void drawRadarLine(Canvas canvas, float radius) {
        Path path = new Path();
        for (int i = 0; i < mValues.length; i++) {
            path.reset();
            path.moveTo(mCenterPoint.x, mCenterPoint.y);
            float x = (float) (mCenterPoint.x + radius * Math.sin(mAngle * i));
            float y = (float) (mCenterPoint.y - radius * Math.cos(mAngle * i));
            path.lineTo(x, y);

            canvas.drawPath(path, mPaint);
        }
    }

    private void drawValuePart(Canvas canvas, float radius) {
        Path path = new Path();
        for (int i = 0; i < mValues.length; i++) {
            float value = mValues[i];
            if (value < 0) value = 0;
            float perRadius = value * radius;
            float x = (float) (mCenterPoint.x + perRadius * Math.sin(mAngle * i));
            float y = (float) (mCenterPoint.y - perRadius * Math.cos(mAngle * i));
            if (i == 0) {
                path.moveTo(x, y);
            } else {
                path.lineTo(x, y);
            }
        }
        path.close();
        canvas.drawPath(path, mPaint);
    }

    private void drawText(Canvas canvas, float radius) {
        for (int i = 0; i < mValues.length; i++) {
            float specialYOffset = 0, specialXOffset = 0;
            String text = mTexts[i];
            float x = (float) (mCenterPoint.x + radius * Math.sin(mAngle * i));
            float y = (float) (mCenterPoint.y - radius * Math.cos(mAngle * i));
            if (i == 0) {
                mTextPaint.setTextAlign(Paint.Align.CENTER);
                specialYOffset = -mTextOffSet;
            } else {
                float temp = i * 1.0f / mValues.length;
                if (temp < 0.5f) {
                    mTextPaint.setTextAlign(Paint.Align.LEFT);
                    specialXOffset = mTextOffSet;
                } else if (temp == 0.5f) {
                    specialYOffset = mTextPaint.getTextSize();
                    mTextPaint.setTextAlign(Paint.Align.CENTER);
                } else {
                    mTextPaint.setTextAlign(Paint.Align.RIGHT);
                    specialXOffset = -mTextOffSet;
                }
                if (y > mCenterPoint.y) {
                    specialYOffset += mTextOffSet;
                } /*else if (y < mCenterPoint.y) {
                    specialYOffset-=mTextOffSet;
                }*/
            }
            canvas.drawText(text, x + specialXOffset, getBaseLine(mTextPaint, y + specialYOffset), mTextPaint);
        }
    }


    private void drawPoints(Canvas canvas, float radius) {
        for (int i = 0; i < mValues.length; i++) {
            float x = (float) (mCenterPoint.x + radius * Math.sin(mAngle * i));
            float y = (float) (mCenterPoint.y - radius * Math.cos(mAngle * i));
            mPaint.setColor(getResources().getColor(mPointColors[i]));
            canvas.drawCircle(x, y, SizeUtil.dp2px(getContext(), 3), mPaint);
        }
    }

    public void setValues(float... values) {
        if (mValues != null && mValues.length != values.length) {
            if (values.length == 5) {
                mTexts = getResources().getStringArray(R.array.RadarArray_5);
                mPointColors = new int[]{
                        R.color.purple_5,
                        R.color.purple_2,
                        R.color.purple_3,
                        R.color.grey_3,
                        R.color.green_light
                };
            } else {
                mTexts = getResources().getStringArray(R.array.RadarArray_6);
                mPointColors = new int[]{
                        R.color.purple_5,
                        R.color.purple_2,
                        R.color.yellow_start,
                        R.color.purple_3,
                        R.color.grey_3,
                        R.color.green_light
                };
            }
        }
        this.mValues = values.clone();
        invalidate();
    }

}
